﻿using System;
using System.Data;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseData;
using BaseData.UI;
using TaxBiz.House;
using TaxBiz.SysConfig;

namespace Admin.House
{
    public partial class ZiyongList : BaseCommon.BasePage
    {
        protected int pageSize = 10;
        protected int page = 1;
        protected int allCount = 0;
        protected string pageUrl = string.Empty;
        protected string strWhere = string.Empty;
        protected string name = string.Empty;//名称
        protected string certificateID = string.Empty; //代码
        protected string houseType = string.Empty; //类型
        protected string diduanID = string.Empty;
        protected string startDate = string.Empty;
        protected string endDate = string.Empty;
        protected string departmentID = string.Empty;
        protected DataTable dtDepartmet = null;
        protected DataTable dtDiduan = null;

        protected RentInfoBiz ddb = new RentInfoBiz();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HasPower())
            {
                Jscript.AlertAndGoBack("您权限不足，不能操作");
                return;
            }
            this.InitData();
            string action = PageContext.GetRequest("action");
            if (!string.IsNullOrEmpty(action))
            {
                if (action.Trim().ToLower() == "stop")
                {
                    Delete();
                }
                else if (action.Trim().ToLower() == "query")
                {
                    //初始化绑定
                    dtDepartmet = new DepartmentBiz().GetList();
                    dtDiduan = new DiDuanBiz().GetList();
                    this.BindData();
                }
                return;
            }
        }

        //删除数据
        protected bool Delete()
        {
            string strID = PageContext.GetRequest("ID");
            string succPageUrl = Server.UrlDecode(Request["pageurl"]);
            int ID = 0;
            int.TryParse(strID, out ID);
            if (ID == 0)
            {
                Jscript.AlertAndGoBack("参数错误");
                return false;
            }
            bool isSucc = ddb.Delete(ID, UserLogin);
            if (!isSucc)
            {
                Jscript.AlertAndGoBack("删除自用房产失败");
                return false;
            }
            Jscript.AlertAndRedirect("删除自用房产成功", succPageUrl);
            return true;
        }

        protected void InitData()
        {
            pageUrl = Request.Url.AbsolutePath;
            //当前页码
            if (PageContext.GetRequest("page") != string.Empty)
            {
                int.TryParse(PageContext.GetRequest("page"), out page);
            }
            pageUrl += "?page=" + page;
            pageUrl += "&action=query";
            //名称
            name = PageContext.GetRequest("txtName");
            if (name != string.Empty)
            {
                pageUrl += "&txtName=" + name;
                strWhere += " and name like '%" + name + "%'";
            }
            certificateID = PageContext.GetRequest("txtCertificateID");
            if (certificateID != string.Empty)
            {
                pageUrl += "&txtCertificateID=" + certificateID;
                strWhere += " and certificateID = '" + certificateID + "'";
            }
            houseType = PageContext.GetRequest("selHouseType");
            if (houseType != string.Empty)
            {
                pageUrl += "&selHouseType=" + houseType;
                strWhere += " and houseType = '" + houseType + "'";
            }
            departmentID = PageContext.GetRequest("selDepart");
            if (departmentID != string.Empty)
            {
                pageUrl += "&selDepart=" + departmentID;
                strWhere += " and CertificateID in (select CertificateID from HouseInfo where DepartmentID=" + departmentID + ")";
            }
            if (isQueryDepartmentOnly)
            {
                strWhere += " and CertificateID in (select CertificateID from HouseInfo where DepartmentID=" + LoginDepartmentID + ")";
            }
            diduanID = PageContext.GetRequest("selDiduan");
            if (diduanID != string.Empty)
            {
                pageUrl += "&selDiduan=" + diduanID;
                strWhere += " and diduanID = " + diduanID;
            }
            DateTime tempDate = DateTime.Now;
            startDate = PageContext.GetRequest("txtStartDate");
            if (!string.IsNullOrEmpty(startDate) && DateTime.TryParse(startDate, out tempDate))
            {
                pageUrl += "&txtStartDate=" + startDate;
                strWhere += " and startDate >= '" + startDate + "'";
            }
            endDate = PageContext.GetRequest("txtEndDate");
            if (!string.IsNullOrEmpty(endDate) && DateTime.TryParse(endDate, out tempDate))
            {
                pageUrl += "&txtEndDate=" + endDate;
                strWhere += " and endDate <= '" + endDate + "'";
            }
            strWhere += " and HouseType2 = '自用'";
        }

        protected void BindData()
        {
            this.rptDataList.DataSource = ddb.GetList(strWhere, pageSize, page, ref allCount);
            this.rptDataList.DataBind();
            //设置分页参数
            this.pageCountrol.CurPage = page;
            this.pageCountrol.PageSize = pageSize;
            this.pageCountrol.AllCount = allCount;
            this.pageCountrol.PageUrl = pageUrl;
            this.pageCountrol.PageName = "page";
            this.pageCountrol.Visible = true;
        }
    }
}